package com.example.medical.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.medical.entity.dto.GetListDto;
import com.example.medical.entity.pojo.DeviceType;
import com.example.medical.entity.pojo.StoreroomType;
import com.example.medical.service.StoreroomTypeService;
import com.example.medical.mapper.StoreroomTypeMapper;
import com.example.medical.utils.common.enums.Code;
import com.example.medical.utils.common.result.PageResponseResult;
import com.example.medical.utils.common.result.Result;
import com.example.medical.utils.config.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author 24354
* @description 针对表【storeroom_type】的数据库操作Service实现
* @createDate 2024-03-31 22:42:30
*/
@Service
public class StoreroomTypeServiceImpl extends ServiceImpl<StoreroomTypeMapper, StoreroomType>
    implements StoreroomTypeService{

    @Override
    public Result AdminGetStoreroomTypeList(GetListDto dto) {
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        IPage<StoreroomType> page=new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<StoreroomType> wrapper=new LambdaQueryWrapper<>();
        if (dto.getMemo()!=null) {
            wrapper.like(StoreroomType::getStoreroomTypeName,dto.getMemo());
        }
        page=page(page,wrapper);
        Result result=new PageResponseResult(dto.getPage(),dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    @Autowired
    private StoreroomTypeMapper storeroomTypeMapper;
    @Override
    public Result AdminRemoveStoreroomTypeArr(String[] ids) {
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        for (String id : ids) {
            boolean checkRemove = removeById(id);
            if (!checkRemove) {
                return Result.errorResult(Code.SERVER_ERROR);
            }
        }
        return Result.okResult(Code.SUCCESS);
    }

    @Override
    public Result AdminUpdateStoreroomType(Long id, String name) {
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        StoreroomType checkStoreroomType = getById(id);
        if (checkStoreroomType==null) {
            return Result.errorResult(Code.DATA_NOT_EXIST);
        }
        checkStoreroomType.setStoreroomTypeName(name);
        boolean checkUpdate = updateById(checkStoreroomType);
        if (!checkUpdate) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        return Result.okResult(Code.SUCCESS);
    }
}




